const user = require("../models/userDetail")
const records = require("../models/records")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false, 'msg': err })
        } else {
            user.find({
                flags: {
                    $all: fields.flags
                }
            }, {
                UUID: 1
            }, (err, docs) => {
                if (err) {
                    res.json({ 'state': false, 'msg': err });
                } else {
                    if (docs.length == 0) {
                        res.json({
                            'state': false,
                            // excercise_list: []
                            'msg': '用户无标签'
                        });
                    } else if (docs.length != 0) {
                        // console.log(docs)
                        let uuid_list = docs.map((ele) => {
                            return ele.UUID
                        })

                        records.find({
                            UUID: {
                                $in: uuid_list
                            },
                            question_ID: fields.question_ID,
                            point_ID: fields.point_ID
                        }, (err, results) => {//获取对应标签学生的知识点id列表 、问题id列表
                            if (err) {
                                res.json({ 'state': false, 'msg': err })
                            } else {
                                // console.log(results);
                                // res.json(results);
                                var right_number = 0;
                                for(let i=0;i<results.length;i++){
                                    if(results[i].result){
                                        right_number++;
                                    }
                                }
                                let right_rate = right_number * 100/results.length;
                                res.json({
                                    'state' : true,
                                    'right_rate' : right_rate
                                })
                            }
                        })
                    }

                }
            })
        }

    });

});

module.exports = router;